import java.util.Scanner;

public class Text3 {
    public static void main(String[] args) {
        //第三题 : 数组中两个字符串的最小距离  算法 : 双指针
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();
        String[] strs = new String[n];
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < n; i++) {
            strs[i] = in.next();
        }

        if(str1 == null || str2 == null) {
            System.out.print(-1);
        }
        int left = -1;
        int right = -1;
        for(int i = 0; i < n; i++) {
            if(strs[i].equals(str1)) {
                min = Math.min(min,right == -1 ? min : i- right);
                left = i;
            }

            if(strs[i].equals(str2)) {
                min = Math.min(min,left == -1 ? min : i-left);
                right = i;
            }
        }
        if(min == Integer.MAX_VALUE) {
            System.out.print(-1);
        }else{
            System.out.print(min);
        }
    }
}
